<?php

use Biz\model\brandActivity\BlindBoxActivity as mBlindBoxActivity;
use Biz\service\wiki\WikiContent;
use Biz\model\brand\Brand;

/**
 * 品牌住友说
 * Class BrandContentAction
 */
class BrandContentAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_NOTHING;

    public function main()
    {
        $page = $this->getRequest()->getQuery('page', 1);
        $currentTime = $this->getRequest()->getQuery('current_time', 0);
        $page = $page > 0 ? $page : 1;
        if ($page == 1 || !$currentTime) {
            $currentTime = time();
        }
        $uid = $this->uid;

        $now = time();
        $where = [
            'activity_state' => mBlindBoxActivity::ACTIVITY_STATE_PASS,
            'start_time[<=]' => $now,
            'end_time[>=]' => $now,
            'brand_id[>]' => 0,
        ];
        $brandIds = mBlindBoxActivity::selectByWhere($where, 'brand_id');
        $brandIds = array_values(array_unique($brandIds));
        if (!$brandIds) {
            $where = [
                'activity_state' => mBlindBoxActivity::ACTIVITY_STATE_PASS,
                'brand_id[>]' => 0,
            ];
            $brandIdsArr = mBlindBoxActivity::selectByWhere($where, 'brand_id');
            $brandIdsArr = array_unique($brandIdsArr);
            $rand_keys = array_rand($brandIdsArr, 3);
            $brandIds = [];
            foreach ($rand_keys as $key) {
                $brandIds[] = $brandIdsArr[$key];
            }
        }
        if ($brandIds) {
            $response = WikiContent::getListForActivity($uid, $brandIds, $page, $currentTime);
        } else {
            $response = [
                'list' => [],
                'is_over' => 1
            ];
        }
        $response['current_time'] = $currentTime;
        $this->setSuccess($response);
    }
}